//
//  ST_EditMyInfoOptionsController.swift
//  btvideo
//
//  Created by mac on 2020/1/9.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import HandyJSON

//个人资料选择项-Controller
class ST_EditMyInfoOptionsController: ST_BTEditStyleBarController {
    
    
    
    var outsideInfo = (selectedId:"",displayValue:"",displayKey:"",modifyKey:"")
    
    
    
    
    //    key value
    @objc var update_value_closure:((String,String)->())?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title_nav_btn.setTitleForAllStates("选择"+outsideInfo.displayKey)
        
        
        view.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.top.equalTo(own_navigation_bar.snp.bottom)
        }
        
        JVrequest(PH.fetchMyInfoOptionsApi,parameters:["search_key":outsideInfo.modifyKey], success: { (json) in
            
            if let list = [ModifyUserItemCellModel].deserialize(from: json["data"].rawString() ?? "") as? [ModifyUserItemCellModel]{
                
                
                var array = [ModifyUserItemCellModel]()
                
                for (idx,item) in list.enumerated(){
                    
                    if item.id == self.outsideInfo.selectedId{
                        var  a = item
                        a.isSelected = true
                        self.last_selected_index = idx
                        array.append(a)
                    }else{
                        array.append(item)
                    }
                }
                
                self.data_array = array
                
                self.collectionView.reloadData()
            }
            
        }) { (code, err) in
            
            print("0------,",err)
            
        }
        
    }
    
    var last_selected_index:Int?
    
    var data_array = [ModifyUserItemCellModel]()
    
    lazy var itemWidth:CGFloat = {
        return ScreenWidth
    }()
    
    lazy var itemHeight = 65.cgFloat
    
    lazy var collectionView: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize.init(width: itemWidth, height: itemHeight)
        
        layout.minimumLineSpacing = 0
        //        layout.minimumInteritemSpacing = 14
        
        let bottomEdge = iPhoneXSeries ? BTSafeAreaBottom + 30 : 20 + 20
        
        layout.sectionInset = UIEdgeInsets.init(top: 0, left: 0, bottom: bottomEdge, right: 0)
        
        let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
        
        collection.register(cellWithClass: ModifyUserItemCell.self)
        
        collection.delegate = self
        
        collection.dataSource = self
        
        collection.alwaysBounceVertical = true
        
        collection.backgroundColor = Color.white
        
        if #available(iOS 11.0, *) {
            collection.contentInsetAdjustmentBehavior = .never
        }
        return collection
    }()
    
    
    override func right_click() {
        
        guard let lastSelected = last_selected_index else {return}
        
        self.update_value_closure?(self.data_array[lastSelected].id,self.data_array[lastSelected].title)
        
        self.goback_previous_controller()
    }
    
    
    
}


extension ST_EditMyInfoOptionsController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return data_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCell(withClass: ModifyUserItemCell.self, for: indexPath)
        
        cell.bind(model: data_array[indexPath.row])
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        
        if indexPath.row != last_selected_index{
            
            if let lastIdx = last_selected_index {
                
                self.data_array[lastIdx].isSelected = false
            }
            
            self.data_array[indexPath.row].isSelected = true
            
            last_selected_index = indexPath.row
            
            self.collectionView.reloadData()
        }
        
    }
    
}

